<!--
 * @Description: zzb
 * @Version: 0.0.1
 * @Autor: zzb
 * @Date: 2022-10-08 15:22:29
 * @LastEditors: zzb
 * @LastEditTime: 2023-10-17 10:48:06
-->
<template>
  <div class="resize-box">
    <div
      class="resize-left"
      id="lineMapBox"
      ref="containLeft"
      style="width: 240px"
    >
      <slot name="left"></slot>
    </div>

    <slot name="center">
      <div class="resize-btn" title="收缩侧边栏">⋮</div>
    </slot>

    <div class="resize-right" ref="containRight">
      <slot name="right"> </slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "Name",

  data() {
    return {};
  },

  components: {},

  computed: {},

  mounted() {
    this.dragControllerDiv();
  },

  watch: {
    value: {
      handler(val) {},
      deep: true,
      immediate: true,
    },
  },

  methods: {
    // 拖拽事件
    dragControllerDiv() {
      console.log("初始化resize");
      var resize = document.getElementsByClassName("resize-btn");
      var left = document.getElementsByClassName("resize-left");
      var mid = document.getElementsByClassName("resize-right");
      var box = document.getElementsByClassName("resize-box");
      for (let i = 0; i < resize.length; i++) {
        // 鼠标按下事件
        resize[i].onmousedown = function (e) {
          //颜色改变提醒
          resize[i].style.background = "#818181";
          var startX = e.clientX;
          resize[i].left = resize[i].offsetLeft;
          // 鼠标拖动事件
          document.onmousemove = (e) => {
            this.toDrag = true;
            var endX = e.clientX;
            var moveLen = resize[i].left + (endX - startX); // （endx-startx）=移动的距离。resize[i].left+移动的距离=左边区域最后的宽度
            var maxT = box[i].clientWidth - resize[i].offsetWidth; // 容器宽度 - 左边区域的宽度 = 右边区域的宽度

            if (moveLen < 32) moveLen = 32; // 左边区域的最小宽度为32px
            if (moveLen > maxT - 150) moveLen = maxT - 150; //右边区域最小宽度为150px

            resize[i].style.left = moveLen; // 设置左侧区域的宽度

            for (let j = 0; j < left.length; j++) {
              left[j].style.width = moveLen + "px";
              mid[j].style.width = box[i].clientWidth - moveLen - 10 + "px";
            }
          };
          // 鼠标松开事件
          document.onmouseup = (evt) => {
            this.toDrag = false;
            //颜色恢复
            resize[i].style.background = "#d6d6d6";
            document.onmousemove = null;
            document.onmouseup = null;
            resize[i].releaseCapture && resize[i].releaseCapture(); //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
          };
          resize[i].setCapture && resize[i].setCapture(); //该函数在属于当前线程的指定窗口里设置鼠标捕获
          return false;
        };
      }
    },
  },
};
</script>

<style scoped lang="scss">
.resize-box {
  display: flex;
  height: 100%;
  width: 100%;
  position: relative;

  .resize-left,
  .resize-right {
    // width: 100%;
    height: 100%;
    // flex: 1;
    position: relative;
    float: left;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .resize-right {
    flex: 1;
    overflow: auto;
  }
  .resize-btn {
    cursor: col-resize;
    float: left;
    position: relative;
    top: 45%;
    background-color: #d6d6d6;
    border-radius: 5px;
    margin-top: -10px;
    width: 10px;
    height: 50px;
    background-size: cover;
    background-position: center;
    /*z-index: 99999;*/
    font-size: 32px;
    color: white;
  }
  .resize-btn:hover {
    color: #444444;
  }
}
</style>
